package com.zhong.gang.app.utils;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhong.gang.applib.bean.MaterialBean;
import com.zhong.gang.applib.utils.NumberUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MaterialView extends LinearLayout implements View.OnClickListener {
    private int type,steelMillId;
    private TextView addView,removeView;

    public void setText(TextView addView, TextView removeView){
        this.addView = addView;
        this.removeView = removeView;
        addView.setOnClickListener(this);
        removeView.setOnClickListener(this);
        showButtonView();
    }

    public MaterialView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setOrientation(VERTICAL);
    }

    public MaterialView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.setOrientation(VERTICAL);
    }

    public int getSteelMillId() {
        return steelMillId;
    }

    public void setType(int steelMillId, int type){
        boolean staus = false;
        if (steelMillId != this.steelMillId || type != this.type){
            removeAllViews();
            staus = true;
        }
        this.steelMillId = steelMillId;
        this.type = type;
        if (steelMillId !=0 && staus) {
            addView();
        }
    }

    public void addView(){
        MaterialItemView materialItemView = new MaterialItemView(getContext(),getChildCount(),steelMillId,type);
        this.addView(materialItemView);
        showButtonView();
    }

    public void addView(MaterialBean bean){
        MaterialItemView materialItemView = new MaterialItemView(getContext(),getChildCount(),bean);
        this.addView(materialItemView);
    }

    public String getJsonArray(){
        JSONArray array = new JSONArray();
        for (int n = 0 ; n < getChildCount();n++){
            View view = getChildAt(n);
            if (view instanceof MaterialItemView) {
                try {
                    MaterialItemView itemView = (MaterialItemView) view;
                    if (itemView.getBean() == null){
                        return null;
                    }
                    String weightStr = itemView.getBinding().edt.getText().toString();
                    if (TextUtils.isEmpty(weightStr))
                        return null;
                    JSONObject object = new JSONObject();
                    object.put("materialTypeId",itemView.getBean().getId()+"");
                    object.put("price","0");
                    object.put("weight", TextUtils.isEmpty(weightStr)?0:weightStr);
                    array.put(object);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
        return array.toString();
    }

    public void removeView(){
        if (getChildCount() <= 1)
            return;
        this.removeViewAt(getChildCount() -1);
        showButtonView();
    }

    private void showButtonView(){
        if (addView == null || removeView == null)
            return;
        if (getChildCount() > 1)
            removeView.setVisibility(VISIBLE);
        else
            removeView.setVisibility(GONE);
        if (getChildCount() >0)
            addView.setVisibility(VISIBLE);
        else
            addView.setVisibility(GONE);
    }

    @Override
    public void onClick(View v) {
        if (v == addView)
            addView();
        else if (v == removeView)
            removeView();
    }
}
